﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;

public partial class Theme_DL_Appraise :PowerCreator.LMS.Web.Base.WebBasePage
{
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.Project.TOCourseList objCourselist = new PowerCreator.LMS.Web.DL.Project.TOCourseList();
    PowerCreator.LMS.Web.Course.Course opCourse = new Course();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    TOPinion opPinion = new TOPinion();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected string classID = "0";
    protected string courseID = "0";
    protected string courseListID = "0";
    private int type = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            this.Title = "培训评价";
            ViewState["type"] = 1;
            ViewState["CourseListID"] = "0";
            ViewState["CourseID"] = "0";
            ViewState["ClassID"] = "0";
            CheckUser();
            if (Request.QueryString["type"] != null)
            {
                type = int.Parse(Request.QueryString["type"]);
                ViewState["type"] = type;
                if (type == 1)
                {
                    if (Request.QueryString["CourseListID"] != null)
                    {
                        courseListID = Request.QueryString["CourseListID"].ToString();
                        ViewState["CourseListID"] = courseListID;
                        classID = Request.QueryString["ClassID"].ToString();
                        ViewState["ClassID"] = classID;
                        BinderPage(type, int.Parse(courseListID));
                    }

                }
                else
                {
                    if (Request.QueryString["CourseID"] != null)
                    {
                        courseID = Request.QueryString["CourseID"].ToString();
                        ViewState["CourseID"] = courseID;
                        classID = Request.QueryString["ClassID"].ToString();
                        ViewState["ClassID"] = classID;
                        BinderPage(type, int.Parse(courseID));
                    }

                }
            }
        }
    }
    private void BinderPage(int type,int clid)
    {
        string sql = "";
        if (type == 1)
        {
            PowerCreator.LMS.DL.Project.Entity.VOTOCourseList vo = objCourselist.Load(clid);
            lbl_ProjectName2.Text = vo.CourseListName;
            sql = " CourseListID=" + clid + " and type=1 and Creator=" + login.UserID.ToString();
            DataTable dt = new DataTable();
            dt = opPinion.Query(sql);
            if (dt.Rows.Count > 0)
            {
                txt_Title.Text = dt.Rows[0]["Title"].ToString();
                txt_Content.Text = dt.Rows[0]["Content"].ToString();
                txt_Scroe.Text = dt.Rows[0]["Score"].ToString();
            }

        }
        else
        {
            PowerCreator.LMS.Course.Entity.VOTOCourse voc = opCourse.LoadVOTCourse(clid);
            lbl_ProjectName2.Text = voc.CourseName;
            sql = " CourseID=" + clid + " and type=0 and Creator=" + login.UserID.ToString();
            DataTable dt = new DataTable();
            dt = opPinion.Query(sql);
            if (dt.Rows.Count > 0)
            {
                txt_Title.Text = dt.Rows[0]["Title"].ToString();
                txt_Content.Text = dt.Rows[0]["Content"].ToString();
                txt_Scroe.Text = dt.Rows[0]["Score"].ToString();
            }

        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }

    protected override void InitPage()
    {
        btnSave.Text = GetLanageValueByKey("btn_EditResourceInfo_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_EditResourceInfo_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "StudyProject.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseListID=" + ViewState["CourseListID"].ToString() + "";
        if (int.Parse(ViewState["type"].ToString()) == 0)
            url = "OpenCourse.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + ViewState["CourseID"].ToString() + "";
        PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
    }
    private bool CheckPageData()
    {
        //string search = string.Format("Title = '{0}'", txt_Title.Text);
        //if (opPinion.Count(search) != 0)
        //{
        //    lbl_msg_Error.Text = "<br />此标题已经存在，请您更换一个标题！";
        //    lbl_msg_Error.Visible = true;
        //    return false;
        //}
        try 
        {
            int score = int.Parse(txt_Scroe.Text.Trim());
        }
        catch(Exception ex)
        {
            lbl_msg_Error.Text = "<br />此标题已经存在，请您更换一个标题！";
            lbl_msg_Error.Visible = true;
            return false;

        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (!CheckPageData()) return;
        if (login != null)
        {
            PowerCreator.LMS.DL.Project.Entity.VOTOPinion vo = new VOTOPinion();


            vo.Title = txt_Title.Text.Trim();
            vo.Content = txt_Content.Text.Trim();
            vo.Score = int.Parse(txt_Scroe.Text.Trim());
            vo.Createtime = DateTime.Now;
            vo.Type = type;
            if (int.Parse(ViewState["type"].ToString()) == 1)
            {
                vo.CourseListID = int.Parse(ViewState["CourseListID"].ToString());


                vo.Creator = login.UserID;
                string sql = "  CourseListID=" + vo.CourseListID.ToString() + " and type=1 and  Creator=" + vo.Creator.ToString();
                DataTable dt = new DataTable();
                dt = opPinion.Query(sql);
                if (dt.Rows.Count > 0)
                {
                    vo.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                    if (opPinion.Update(vo))
                    {
                        string url = "StudyProject.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseListID=" + ViewState["CourseListID"].ToString() + "";
                        Script.ShowAndRedirect(this, "操作成功", url);
                    }
                }
                else
                {
                    if (opPinion.Add(vo))
                    {
                        string url = "StudyProject.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseListID=" + ViewState["CourseListID"].ToString() + "";
                        Script.ShowAndRedirect(this, "操作成功", url);

                    }
                }
            }
            else
            {
                vo.CourseID = int.Parse(ViewState["CourseID"].ToString());
                vo.Creator = login.UserID;
                string sql = "  CourseID=" + vo.CourseListID.ToString() + " and type=0 and  Creator=" + vo.Creator.ToString();
                DataTable dt = new DataTable();
                dt = opPinion.Query(sql);
                if (dt.Rows.Count > 0)
                {
                    vo.ID = int.Parse(dt.Rows[0]["ID"].ToString());
                    if (opPinion.Update(vo))
                    {
                        string url = "OpenCourse.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + ViewState["CourseID"].ToString() + "";
                        Script.ShowAndRedirect(this, "操作成功", url);
                    }
                }
                else
                {
                    if (opPinion.Add(vo))
                    {
                        string url = "OpenCourse.aspx?ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + ViewState["CourseID"].ToString() + "";
                        Script.ShowAndRedirect(this, "操作成功", url);

                    }
                }
            }

        }
        else
        {
            Script.RedirectURL(url);

        }

     
    }
}
